package io.wit.common.utils;

import io.wit.common.xss.SQLFilter;
import io.wit.modules.sys.entity.SysUserEntity;
import io.wit.modules.sys.shiro.ShiroUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询参数
*/
public class Query extends LinkedHashMap<String, Object> {

    private static final long serialVersionUID = 1L;
	//当前页码
    private int page;
    //每页条数
    private int limit;

    public Query(final Map<String, Object> params){
        final Map<String,Object> query = new HashMap<>();
        if(params != null && params.size() >0){
            for(final String k : params.keySet()){
                final Object o = params.get(k);
                if(o != null){
                    if(o instanceof String){
                        final String value = (String) o;
                        if(k.equalsIgnoreCase("_")) continue;
                        if(k.equalsIgnoreCase("nd")) continue;
                        if(k.equalsIgnoreCase("_search")) continue;
                        if(value.length() > 0){
                            query.put(k,value);
                        }
                    }else{
                        query.put(k,o);
                    }
                }
            }
        }
        this.putAll(query);
        if(query.containsKey("page") && query.containsKey("limit")){
            //分页参数
            this.page = Integer.parseInt((String)query.get("page"));
            this.limit = Integer.parseInt((String)query.get("limit"));
            this.put("offset",(page - 1) * limit);
            this.put("page",page);
            this.put("limit",limit);
        }
        //防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
        final String sidx = (String) query.get("sidx");
        final String order = (String) query.get("order");
        if(sidx != null && sidx.length() > 0){
            this.put("sidx",SQLFilter.sqlInject(sidx));
            this.put("order",SQLFilter.sqlInject(order));
        }else{
            this.remove("sidx");
            this.remove("order");
        }
        final SysUserEntity user = ShiroUtils.getUserEntity();
        final Long roleId = user.getRoleId();
        final Integer options = user.getOptions();
        if(roleId != null){
            if(options == 2 || options == 3){//是否是代理或管理部门
                final String areas = user.getAreaIds();
                if(areas != null){
                    final List<Long> listArea = new ArrayList<>();
                    final String[] arrays = areas.split(",");
                    for(int x = 0; x < arrays.length; x++){
                        listArea.add(Long.parseLong(arrays[x]));
                    }
                    if(listArea.size()>0){
                        this.put("listArea",listArea);
                    }
                }
            }else{
                final Long subjectId = user.getSubjectId();
                if(subjectId != null){
                    this.put("subjectId",user.getSubjectId());
                }
                final Long deptId = user.getDeptId();
                if(deptId != null){
                    this.put("deptId",deptId);
                }
            }
        }
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }
}
